/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details:
 * http://www.gnu.org/copyleft/lesser.html#TOC3
 */
package com.simplemonkey.widgets;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import com.jme.image.Texture;

public class ToggleButton extends TextureWidget {
	private List<ToggleListener> listeners = new CopyOnWriteArrayList<ToggleListener>();
	
	private boolean value = false;
	private String name;
	private Texture activeTexture;
	private Texture inactiveTexture;
	private Texture hoverTexture;
	private int defaultUnit = 1;
	
	public ToggleButton(String name,Texture activeTexture, Texture inactiveTexture,Texture hoverTexture) {
		this(name,activeTexture,inactiveTexture,hoverTexture,1);
	
	}
		
	
	public ToggleButton(String name,Texture activeTexture, Texture inactiveTexture,Texture hoverTexture,int unit) {
		super(name);
		defaultUnit= unit;
		this.activeTexture = activeTexture;
		this.inactiveTexture = inactiveTexture;
		this.hoverTexture = hoverTexture;
		this.name = name;
		this.setSize(inactiveTexture.getImage().getWidth(), inactiveTexture.getImage().getHeight());
		this.setValue(this.isValue());
	}
	
	public ToggleButton(String name,Texture activeTexture, Texture inactiveTexture)
	{
		this(name,activeTexture,inactiveTexture,inactiveTexture);
	}
	
	public void addToggleListener(ToggleListener listener)
	{
		listeners.add(listener);
	}
	
	public void removeToggleListener(ToggleListener listener)
	{
		listeners.remove(listener);
	}
	
	public void clearListeners()
	{
		listeners.clear();
	}
	
	@Override
	public String toString() {
		return name;
	}

	@Override
	public void mousePress(boolean pressed, int button, float x, float y) {
		super.mousePress(pressed, button, x, y);
		if(pressed && button == 0)
		{
			setValue(!isValue());
		}
	}

	public boolean isValue() {
		return value;
	}

	public void setValue(boolean value) {
		if(this.value != value)
		{
			this.value = value;
			for (ToggleListener listener:listeners)
			{
				listener.toggle(value);
			}
		}
		
		if(value)
		{
			setTexture(activeTexture,defaultUnit);
		}else
		{
			setTexture(inactiveTexture,defaultUnit);
		}
	}

	@Override
	public void mouseMove(float x, float y) {
		if(!isValue() && this.getTexture(defaultUnit) != hoverTexture)
			this.setTexture(hoverTexture,defaultUnit);
		super.mouseMove(x, y);
	}

	@Override
	public void mouseOff() {
		if(!isValue())
			this.setTexture(inactiveTexture,defaultUnit);
		super.mouseOff();
	}


	public String getName() {
		return name;
	}
	
	
	
}
